package exportimporterentities;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;

import org.apache.log4j.Logger;

import entities.Garage;
import entities.IGarage;
import entities.IMaster;
import entities.IOrder;
import entities.Master;
import entities.Order;
import entities.OrderStatus;

public class Import {
	private Scanner garageReader;
	private Scanner masterReader;
	private Scanner orderReader;
	private static final String FAIL = "Sorry, file, not found";
	private static final String DATE_FORMAT = "dd.MM.yyyy";
	private static final String DELETED = "DELETED";
	private static final String CANCELED = "CANCELED";
	private static final String MADE = "MADE";
	private ArrayList<IOrder> arrayOrder = new ArrayList<IOrder>();
	private ArrayList<IMaster> arrayMaster = new ArrayList<IMaster>();
	private ArrayList<IGarage> arrayGarage = new ArrayList<IGarage>();
	private static final String GARAGE_PATH_CSV="garage.csv";
	private static final String MASTER_PATH_CSV="master.csv";
	private static final String ORDER_PATH_CSV="order.csv";
	private static final String WRONG_DATE_FORMAT="Wrong date format";
	private Logger log= Logger.getLogger(Import.class);

	Import() {
		try {
			garageReader = new Scanner(new File(GARAGE_PATH_CSV));
			masterReader = new Scanner(new File(MASTER_PATH_CSV));
			orderReader = new Scanner(new File(ORDER_PATH_CSV));
		} catch (IOException e) {
			log.fatal(FAIL);
			throw new RuntimeException(FAIL);		}
	}

	ArrayList<IGarage> getGarages() {
		String temp[] = new String[2];
		while (garageReader.hasNextLine()) {
			String s = garageReader.nextLine();
			temp = s.split(" ");
			IGarage garage = new Garage(Integer.parseInt(temp[1]));
			garage.setId(Integer.parseInt(temp[0]));
			arrayGarage.add(garage);
		}
		garageReader.close();
		return arrayGarage;
	}

	ArrayList<IMaster> getMasters() {
		String temp[] = new String[2];
		while (masterReader.hasNextLine()) {
			String s = masterReader.nextLine();
			temp = s.split(" ");
			IMaster master = new Master(temp[1]);
			master.setId(Integer.parseInt(temp[0]));
			arrayMaster.add(master);
		}

		masterReader.close();
		return arrayMaster;
	}

	ArrayList<IOrder> getOrders() {
		String temp[] = new String[8];
		HashMap<Integer, Integer> masterTemp = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> garageTemp = new HashMap<Integer, Integer>();
		while (orderReader.hasNextLine()) {
			String s = orderReader.nextLine();
			temp = s.split(" ");
			IOrder order = new Order();
			order.setId(Integer.parseInt(temp[0]));
			order.setNumberOrder(Integer.parseInt(temp[1]));
			order.setFillingDate(parser(temp[2]));
			order.setDeadlineDate(parser(temp[3]));
			order.setBeginingDate(parser(temp[4]));
			order.setPrice(Integer.parseInt(temp[5]));
			if (temp[6].equals(CANCELED)) {
				order.setOrderStatus(OrderStatus.CANCELED);
			} else if (temp[6].equals(DELETED)) {
				order.setOrderStatus(OrderStatus.DELETED);
			} else if (temp[6].equals(MADE)) {
				order.setOrderStatus(OrderStatus.MADE);
			}
			arrayOrder.add(order);
			masterTemp.put(order.getNumberOrder(), Integer.parseInt(temp[7]));
			garageTemp.put(order.getNumberOrder(), Integer.parseInt(temp[8]));

		}

		joinData(masterTemp, garageTemp);
		orderReader.close();
		return arrayOrder;
	}

	private Date parser(String h) {
		SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
		Date date= new Date();
		try {
		  date = f.parse(h);
		} catch (ParseException pe) {
			log.error(WRONG_DATE_FORMAT);
			throw new RuntimeException(WRONG_DATE_FORMAT);
		}
		return date;

	}

	private void joinData(HashMap<Integer, Integer> masterTemp,
			HashMap<Integer, Integer> garageTemp) {
		getGarages();
		getMasters();
		for (IOrder order : arrayOrder) {
			for (IMaster master : arrayMaster) {
				for (IGarage garage : arrayGarage) {
					if (masterTemp.get(order.getNumberOrder()) == master.getId()
							&& garageTemp.get(order.getNumberOrder()) == garage
									.getId()) {
						order.sendMasterGarage(master, garage);
					}
				}
			}
		}
	}

}
